﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.ObjectModel;

using InfoKiosk;
using TFSServices.TFS;

using Visifire.Charts;
using Visifire.Commons;

namespace TFSServices
{
    public partial class TfsStatusControl : UserControl, IInfoPage
    {
        public TfsStatusControl()
        {
            InitializeComponent();
        }

        public void Start()
        {
            Binding binding = new BasicHttpBinding();

            String tfsServiceURI = ComponentParameters.Where(
                param => param.Key == "TFSServiceURI").First().Value;

            TfsServiceClient client = new TfsServiceClient(binding,
                new EndpointAddress(tfsServiceURI));

            String tfsServerName = ComponentParameters.Where(
                param => param.Key == "TFSServer").First().Value;

            String workItemTypeName = ComponentParameters.Where(
                param => param.Key == "TFSWorkItemTypeName").First().Value;

            client.GetProjectStatusCompleted += client_GetProjectStatusCompleted;
            client.GetProjectStatusAsync(tfsServerName, workItemTypeName);
        }

        void client_GetProjectStatusCompleted(object sender, GetProjectStatusCompletedEventArgs e)
        {
            String workItemTypeName = ComponentParameters.Where(
                param => param.Key == "TFSWorkItemTypeName").First().Value;

            TeamProject[] teamProjects = e.Result.ToArray<TeamProject>();

            CreateChart(teamProjects, workItemTypeName);
        }

        public List<IComponentParameter> ComponentParameters { get; set; }

        public void CreateChart(
            TeamProject[] teamProjects,
            String workItemTypeName)
        {
            if (teamProjects == null)
            {
                return;
            }

            Chart visiChart = new Chart();
            DataSeries activeItemsDS = new DataSeries();
            DataSeries resolvedItemsDS = new DataSeries();
            DataSeries closedItemsDS = new DataSeries();
            Title title = new Title();
            AxisX axisX = new AxisX();
            AxisY axisY = new AxisY();

            title.Text = "TFS Project Status";
            visiChart.Children.Add(title);
            axisX.Title = "TFS Projects";
            visiChart.Children.Add(axisX);
            axisY.Title = String.Format(
                "Total number of {0}s", workItemTypeName);
            visiChart.Children.Add(axisY);


            Int32 containerHeight = (Int32)((StackPanel)((TfsStatusControl)LayoutRoot.Parent).Parent).ActualHeight;
            Int32 containerWidth = (Int32)((StackPanel)((TfsStatusControl)LayoutRoot.Parent).Parent).ActualWidth;

            visiChart.Width = containerWidth - 20; // Width and Height are required for the Chart.
            visiChart.Height = containerHeight - 20;

            // Active bugs
            foreach (TeamProject project in teamProjects)
            {
                // Add closed bugs
                DataPoint dataPoint = new DataPoint();
                dataPoint.AxisLabel = project.Name;
                dataPoint.YValue = project.ClosedItems;

                if (project.ClosedItems > 0)
                {
                    dataPoint.LabelEnabled = "True";
                    dataPoint.LabelText = Convert.ToString(project.ClosedItems);
                }

                closedItemsDS.Children.Add(dataPoint);

                // Add resolved bugs
                dataPoint = new DataPoint();
                dataPoint.YValue = project.ResolvedItems;

                if (project.ResolvedItems > 0)
                {
                    dataPoint.LabelEnabled = "True";
                    dataPoint.LabelText = Convert.ToString(project.ResolvedItems);
                }

                resolvedItemsDS.Children.Add(dataPoint);

                // Add active bugs
                dataPoint = new DataPoint();
                dataPoint.YValue = project.ActiveItems;

                if (project.ActiveItems > 0)
                {
                    dataPoint.LabelEnabled = "True";
                    dataPoint.LabelText = Convert.ToString(project.ActiveItems);
                    dataPoint.LabelFontColor = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x00, 0x00, 0x00));
                }

                activeItemsDS.Children.Add(dataPoint);
            }

            closedItemsDS.RenderAs = "StackedColumn";
            closedItemsDS.SetValue(
                NameProperty,
                String.Format(
                    "Closed {0}s",
                    workItemTypeName));
            visiChart.Children.Add(closedItemsDS);

            resolvedItemsDS.RenderAs = "StackedColumn";
            resolvedItemsDS.SetValue(NameProperty,
                String.Format(
                    "Resolved {0}s",
                    workItemTypeName));
            visiChart.Children.Add(resolvedItemsDS);

            activeItemsDS.RenderAs = "StackedColumn";
            activeItemsDS.SetValue(NameProperty,
                String.Format(
                    "Active {0}s",
                    workItemTypeName));
            visiChart.Children.Add(activeItemsDS);

            LayoutRoot.Children.Clear(); // So that Chart objects don’t build up each time we add one.
            LayoutRoot.Children.Add(visiChart);
        }
    }
}
